<?php
/**
 * 医生管理
 * Created by PhpStorm.
 * User: dower
 * Date: 2016/7/26 0026
 * Time: 下午 15:31
 */
namespace Web\Controller\Doctor;

use Web\Model\Doctor\DoctorModel;
class DoctorController extends BaseController{

    public $model = null;
    private $size = 10;
    public $userinfo = null;

    public function __construct()
    {
        parent::__construct();
        $this->handleLogin(true);//该控制器所以操作均要求登录情况下
        $this->model = new DoctorModel();
    }

    /**
     * 医生首页
     */
    public function index(){
        //加载视图
        $this->display();
    }

    /*-----------------我要看病------------------*/

    /**
     * 我要看病--患者列表(已诊疗)
     */
    public function patientlist(){
        //添加查询条件
        $mobile = I('get.mobile');
        if($mobile!==''){
            $where['b.mobile'] = $mobile;
        }
        $where['a.doctor_id'] = UID;
        //得到列表
        $list = $this->model->patientList($where,$this->size);
        $this->assign('mobile', $mobile);
        $this->assign('pageHtml', $list['pageHtml']);
        $this->assign('list', $list['rows']);
        $this->assign('count',$list['count']);
        //设置头部
        $my_header = array(
            'h_cur'=>1,
        );
        $my_header = array_merge($this->header,$my_header);
        $this->assign($my_header);
        //加载视图
        $this->display('looking.patientlist');
    }

    /**
     * 添加诊疗记录
     * @param $id int
     */
    public function addtreatment($id=0){
        if(IS_POST){
            $params = I('post.');
            if(empty($params['treat_time'])){ //默认时间为当前时间
                $params['treat_time'] = date('Y-m-d');
            }
            $rst = M('DoctorTreatment')->add($params);
            if($rst===false){
                $this->ajaxReturn(array('code'=>1,'msg'=>'添加诊疗信息失败'));
            }else{
                //更新医生表
                D('CommonDoctor')->putToCount(1,UID,0,$this->userinfo['clinic_id']);
                $this->ajaxReturn(array('code'=>0,'url'=>U('patientlist')));
            }
        }else{
            //得到患者名
            $patient = M('Patient')->where(array('id'=>$id))->getField('name');
            $this->assign('patient',$patient);
            $this->assign('userinfo',$this->userinfo);
            //设置头部
            $my_header = array(
                'h_cur'=>1,
            );
            $my_header = array_merge($this->header,$my_header);
            $this->assign($my_header);
            //加载视图
            $this->display('looking.addtreatment');
        }
    }

    /**
     * 查看患者
     * @param $id
     */
    public function viewpatient($id){
        //得到该患者最近的一次的诊疗情况
        $info = M('DoctorTreatment')->find($id);
        //得到诊所名
        if($info){
            //得到医生所属诊所
            $clinic_id = M('Doctor')->where(array('id'=>$info['doctor_id']))->getField('clinic_id');
            if($clinic_id){
                $clinic_name = M('Clinic')->where(array('id'=>$clinic_id))->getField('clinic_name');
                if($clinic_name) $info['clinic_name'] = $clinic_name;
            }
        }
        $this->assign('treatment',$info);
        //得到患者名
        $patient = M('Patient')->where(array('id'=>$info['patient_id']))->getField('name');
        $this->assign('patient',$patient);
        //设置头部
        $my_header = array(
            'h_cur'=>1
        );
        $my_header = array_merge($this->header,$my_header);
        $this->assign($my_header);
        //加载视图
        $this->display('looking.viewpatient');
    }

    /**
     * 添加患者
     */
    public function addpatient(){
        if(IS_POST){
            $params = I('post.');
            $rst = $this->model->addPatient($params);
            if($rst===false){
                $this->ajaxReturn(array('code'=>1,'msg'=>$this->model->getError()));
            }else{
                $this->ajaxReturn(array('code'=>0,'url'=>U('patientlist')));
            }
        }else{
            //设置头部
            $my_header = array(
                'h_cur'=>1,
            );
            $my_header = array_merge($this->header,$my_header);
            $this->assign($my_header);
            $this->assign('userinfo',$this->userinfo);
            //加载视图
            $this->display('looking.addpatient');
        }
    }


    /*-----------------我的患者------------------*/

    /**
     * 我的患者
     */
    public function patients(){
        //得到列表
        $list = $this->model->myPatients();
        $this->assign('list', $list['rows']);
        $this->assign('count',$list['count']);
        //设置头部
        $my_header = array(
            'h_cur'=>2,
        );
        $my_header = array_merge($this->header,$my_header);
        $this->assign($my_header);
        //加载视图
        $this->display('patient.patients');
    }

    /**
     * 查看患者详情
     * @param $id
     */
    public function patient($id){
        $patient = M('Patient')->where(array('id'=>$id))->find();
        $this->assign('patient',$patient);
        //设置头部
        $my_header = array(
            'h_cur'=>2,
        );
        $my_header = array_merge($this->header,$my_header);
        $this->assign($my_header);
        //加载视图
        $this->display('patient.patient');
    }

    /**
     * 患者诊疗记录
     * @param $id int
     */
    public function patient_treatmentlist($id){
        $where['a.patient_id'] = $id;
        //得到列表
        $list = $this->model->patientTreatmentList($where);
        //处理信息
        $this->assign('count', $list['count']);
        $this->assign('list', $list['rows']);
        //得到患者信息
        $patient = M('Patient')->field('name,age,sex,remark')->where(array('id'=>$id))->find();
        $this->assign('patient',$patient);
        //设置头部
        $my_header = array(
            'h_cur'=>2,
        );
        $my_header = array_merge($this->header,$my_header);
        $this->assign($my_header);
        //加载视图
        $this->display('patient.treatmentlist');
    }

    /**
     * 查看诊疗详情
     * @param $id int 诊疗id
     */
    public function viewtreatment($id){
        //查询详情
        $treatment = M('DoctorTreatment')->alias('a')->field('a.*,b.clinic_name as clinic,c.name as doctor')->where(array('a.id='.$id))->join(' LEFT JOIN __DOCTOR__ c on a.doctor_id=c.id LEFT JOIN __CLINIC__ b on c.clinic_id=b.id')->find();
        $this->assign($treatment);
        //设置头部
        $my_header = array(
            'h_cur'=>0,
        );
        $my_header = array_merge($this->header,$my_header);
        $this->assign($my_header);
        //加载视图
        $this->display('patient.viewtreatment');
    }

    /*-----------------我的诊疗记录------------------*/

    /**
     * 我的诊疗
     */
    public function treatmentlist(){
        //添加查询条件
        $name = I('get.name');
        if($name!==''){
            $where['b.name'] = array('like','%'.$name.'%');
        }
        $where['a.doctor_id'] = UID;
        //得到列表
        $list = $this->model->treatmentlist($where,$this->size);
        $this->assign('size', $this->size);
        $this->assign('pageHtml', $list['pageHtml']);
        $this->assign('count', $list['count']);
        $this->assign('list', $list['rows']);
        //设置头部
        $my_header = array(
            'h_cur'=>3,
        );
        $my_header = array_merge($this->header,$my_header);
        $this->assign($my_header);
        $this->assign('name',$name);
        //加载视图
        $this->display('treatment.list');
    }


    /**
     * 诊疗数据
     */
    public function treatmentcount(){
        //查询总量
        $where['doctor_id'] = UID;
        $total = M('DoctorTreatment')->where($where)->count();
        //添加查询条件
        $date = I('get.date');
        $date = $date?:date('Y-m');//默认为当前月
        $sql = "select count(*) as count from __PREFIX__doctor_treatment where `doctor_id`=".UID." and DATE_FORMAT(`treat_time`, '%Y-%m')='{$date}'";
        $rst = M('DoctorTreatment')->query($sql);
        $count = 0;
        if($rst){
            $count = $rst[0]['count'];
        }
        $this->assign('date',$date);
        $this->assign('total',$total);
        $this->assign('count',$count);
        //设置头部
        $my_header = array(
            'h_cur'=>3,
        );
        $my_header = array_merge($this->header,$my_header);
        $this->assign($my_header);
        //加载视图
        $this->display('treatment.count');
    }

    /**
     * ajax获得月份数量
     * @param $date
     */
    public function getTreatmentCount($date){
        //查询数量
        $date = $date?:date('Y-m');//默认为当前月
        $sql = "select count(*) as count from __PREFIX__doctor_treatment where `doctor_id`=".UID." and DATE_FORMAT(`treat_time`, '%Y-%m')='{$date}'";
        $rst = M('DoctorTreatment')->query($sql);
        $count = 0;
        if($rst){
            $count = $rst[0]['count'];
        }
        $this->ajaxReturn(array('code'=>0,'data'=>$count));
    }

    /*-----------------患者评价------------------*/

    /**
     * 患者评价
     */
    public function assesslist(){
        $where = array('a.doctor_id'=>UID);
        //得到列表
        $list = $this->model->assessList($where,$this->size);
        $this->assign('list', $list['rows']);
        $this->assign('pageHtml',$list['pageHtml']);
        //设置头部
        $my_header = array(
            'h_cur'=>6,
        );
        $my_header = array_merge($this->header,$my_header);
        $this->assign($my_header);
        //加载视图
        $this->display('assess.list');
    }

    /**
     * 评价数据
     */
    public function assesscount(){
        //查询总量
        $where['doctor_id'] = UID;
        //得到总数
        $total = $this->model->assessData($where);
        //得到每月的分析数据
        $date = I('get.date');
        $date = $date?:date('Y-m');//默认为当前月
        $count = $this->model->assessData($where,2,$date);
        $this->assign('total',$total);
        $this->assign('count',count($count));
        $this->assign('date',$date);
        //设置头部
        $my_header = array(
            'h_cur'=>6,
        );
        $my_header = array_merge($this->header,$my_header);
        $this->assign($my_header);
        //加载视图
        $this->display('assess.count');
    }

    /**
     * ajax获得月份数量
     * @param $date
     */
    public function getAssess($date){
        //查询总量
        $where['doctor_id'] = UID;
        //查询数量
        $date = $date?:date('Y-m');//默认为当前月
        $list = $this->model->assessData($where,2,$date);
        $this->ajaxReturn(array('count'=>count($list),'data'=>$list));
    }

    /*-----------------咨询我的------------------*/

    /**
     * 咨询列表
     */
    public function consultlist(){
        $where = array('a.doctor_id'=>UID);
        //得到列表
        $list = $this->model->consultList($where);
        $this->assign('list', $list['rows']);
        //设置头部
        $my_header = array(
            'h_cur'=>5,
        );
        $my_header = array_merge($this->header,$my_header);
        $this->assign($my_header);
        //加载视图
        $this->display('consult.list');
    }

    /**
     * 咨询统计
     */
    public function consultcount(){
        //查询总量
        $where['doctor_id'] = UID;
        $total = M('DoctorConsult')->where($where)->count();
        //添加查询条件
        $date = I('get.date');
        $date = $date?:date('Y-m');//默认为当前月
        $sql = "select count(*) as count from __PREFIX__doctor_consult where `doctor_id`=".UID." and DATE_FORMAT(`date`, '%Y-%m')='{$date}'";
        $rst = M('DoctorConsult')->query($sql);
        $count = 0;
        if($rst){
            $count = $rst[0]['count'];
        }
        $this->assign('date',$date);
        $this->assign('total',$total);
        $this->assign('count',$count);
        //设置头部
        $my_header = array(
            'h_cur'=>5,
        );
        $my_header = array_merge($this->header,$my_header);
        $this->assign($my_header);
        //加载视图
        $this->display('consult.count');
    }

    /**
     * ajax获得月份数据
     * @param $date
     */
    public function getConsult($date){
        //查询数量
        $date = $date?:date('Y-m');//默认为当前月
        $sql = "select a.*,b.name from __PREFIX__doctor_consult as a join __PREFIX__patient as b on a.patient_id=b.id where a.`doctor_id`=".UID." and DATE_FORMAT(a.`date`, '%Y-%m')='{$date}'";
        $rst = M('DoctorConsult')->query($sql);
        $count = 0;
        $rows = array();
        if($rst){
            $count = count($rst);
            $rows = $rst;
        }
        $this->ajaxReturn(array('count'=>$count,'data'=>$rows));
    }

    /*-----------------预约我的------------------*/

    /**
     * 预约列表
     */
    public function appointlist(){
        $where = array('a.doctor_id'=>UID);
        //得到列表
        $list = $this->model->appointList($where);
        $this->assign('list', $list['rows']);
        //设置头部
        $my_header = array(
            'h_cur'=>4,
        );
        $my_header = array_merge($this->header,$my_header);
        $this->assign($my_header);
        //加载视图
        $this->display('appoint.list');
    }

    /**
     * 预约统计
     */
    public function appointcount(){
        //查询总量
        $where['doctor_id'] = UID;
        $total = M('DoctorAppoint')->where($where)->count();
        //添加查询条件
        $date = I('get.date');
        $date = $date?:date('Y-m');//默认为当前月
        $sql = "select count(*) as count from __PREFIX__doctor_appoint where `doctor_id`=".UID." and DATE_FORMAT(`time`, '%Y-%m')='{$date}'";
        $rst = M('DoctorAppoint')->query($sql);
        $count = 0;
        if($rst){
            $count = $rst[0]['count'];
        }
        $this->assign('date',$date);
        $this->assign('total',$total);
        $this->assign('count',$count);
        //设置头部
        $my_header = array(
            'h_cur'=>4,
        );
        $my_header = array_merge($this->header,$my_header);
        $this->assign($my_header);
        //加载视图
        $this->display('appoint.count');
    }

    /**
     * ajax获得月份
     * @param $date
     */
    public function getAppoint($date){
        //查询数量
        $date = $date?:date('Y-m');//默认为当前月
        $sql = "select a.*,b.name from __PREFIX__doctor_appoint as a join __PREFIX__patient as b on a.patient_id=b.id where a.`doctor_id`=".UID." and DATE_FORMAT(a.`time`, '%Y-%m')='{$date}'";
        $rst = M('DoctorAppoint')->query($sql);
        $count = 0;
        $rows = array();
        if($rst){
            $count = count($rst);
            $rows = $rst;
        }
        $this->ajaxReturn(array('count'=>$count,'data'=>$rows));
    }

    /**
     * 疾病库
     * @param int $type
     */
    public function disease($type=1){
        if($type==1){ //科室分类
            $category = 0;
        }else{ //分群分类
            $category = 1;
        }
        //得到相关信息
        $list = S('my_disease_'.$category);
        if(empty($list)){
            $list = $this->model->getCate($category);
            if($list) S('my_disease_'.$category,$list,C('MY_CACHE_TIME'));//缓存结果
        }
        //设置头部
        $my_header = array(
            'h_cur'=>7,
        );
        $my_header = array_merge($this->header,$my_header);
        $this->assign($my_header);
        //分配其他信息
        $this->assign('list',$list);
        $this->assign('type',$type);
        //加载视图
        $this->display('doc.cate');
    }

    /**
     * 得到疾病的详情
     * @param $id
     */
    public function diseasedetail($id){
        //得到疾病具体信息
        $info = M('Disease')->find($id);
        if($info){
            foreach($info as $k=>&$v){
                if($v && $k!='disease_name'){
                    $v=base64_decode($v);
                }
            }
            unset($v);
        }
        //设置头部
        $my_header = array(
            'h_cur'=>7,
        );
        $my_header = array_merge($this->header,$my_header);
        $this->assign($my_header);
        //分配其他信息
        $this->assign('info',$info);
        //加载视图
        $this->display('doc.catedetail');
    }

    /**
     * 名方验方
     */
    public function recipe(){
        //获得信息
        $name = I('get.name');
        $where = array();
        if($name){ //搜索时才获取数据
            $where['prescription_name'] = array('like',"%{$name}%");
        }
        //查询数据
        $list = $this->model->recipeList($where,$this->size);
        $this->assign('list', $list['rows']);
        $this->assign('pageHtml',$list['pageHtml']);
        //设置头部
        $my_header = array(
            'h_cur'=>8,
        );
        $my_header = array_merge($this->header,$my_header);
        $this->assign($my_header);
        //分配其他信息
        $this->assign('name',$name);
        //加载视图
        $this->display('doc.recipe');
    }

    /**
     * 名方验方详情
     * @param $id
     */
    public function recipedetail($id){
        //得到疾病具体信息
        $info = M('Prescription')->find($id);
        //设置头部
        $my_header = array(
            'h_cur'=>8,
        );
        $my_header = array_merge($this->header,$my_header);
        $this->assign($my_header);
        //分配其他信息
        $this->assign('info',$info);
        //加载视图
        $this->display('doc.recipedetail');
    }

    /**
     * 系统消息
     */
    public function news(){
        //得到系统消息及个人消息
        $where = array('accept_id'=>array('in','0,'.UID));
        $where['accept_id'] = UID;
        $where['type'] = 1;
        $map['_complex'] = $where;
        $map['_logic'] = 'or';
        $map['accept_id'] = 0;
        $list = $this->model->newsList($where,$this->size);
        $this->assign('list',$list['rows']);
        $this->assign('pageHtml',$list['pageHtml']);
        //设置头部
        $my_header = array(
            'h_cur'=>9,
        );
        $my_header = array_merge($this->header,$my_header);
        $this->assign($my_header);
        //加载视图
        $this->display();
    }

    /**
     * 系统消息详情
     * @param $id
     */
    public function news_detail($id){
        //得到消息
        $info = M('SystemMessage')->find($id);
        //设置头部
        $my_header = array(
            'h_cur'=>9,
        );
        $my_header = array_merge($this->header,$my_header);
        $this->assign($my_header);
        //分配其他信息
        $this->assign('info',$info);
        //加载视图
        $this->display();
    }

}